<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称" style="font-weight: bold; color: #606266;">{{ spu.spuName }}</el-form-item>
      <el-form-item label="SKU名称">
        <el-input v-model="skuInfo.skuName" placeholder="SKU名称" />
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input v-model="skuInfo.price" placeholder="价格(元)" type="number" />
      </el-form-item>
      <el-form-item label="重量(KG)">
        <el-input v-model="skuInfo.weight" placeholder="重量(KG)" />
      </el-form-item>
      <el-form-item label="规则描述">
        <el-input v-model="skuInfo.skuDesc" type="textarea" rows="4" name="" placeholder="规则描述" />
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item v-for="(attr) in attrInfoList" :key="attr.id" :label="attr.attrName">
            <el-select v-model="attr.attrIdAndValueId" placeholder="请选择" style="margin-bottom: 15px">
              <el-option v-for="(attrValue) in attr.attrValueList" :key="attrValue.id" :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item v-for="(saleAttr) in spuSaleAttrList" :key="saleAttr.id" :label="saleAttr.saleAttrName">
            <el-select v-model="saleAttr.attrIdAndValueId" placeholder="请选择" style="margin-bottom: 15px">
              <el-option
                v-for="(saleAttrValue) in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table style="width: 100%" border :data="spuImageList" @selection-change="handleCurrentChange">
          <el-table-column type="selection" prop="prop" width="80" align="center" />
          <el-table-column prop="prop" label="图标" width="width">
            <template slot-scope="{row}">
              <img :src="row.imgUrl" style="width: 100px; height: 100px">
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width" />
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row}">
              <el-button v-if="row.isDefault===0" type="primary" @click="changeDefault(row)">设置默认</el-button>
              <el-button v-else type="success">默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'SkuForm',
  data() {
    return {
      spuImageList: [],
      spuSaleAttrList: [],
      attrInfoList: [],
      // 收集sku数据字段
      skuInfo: {
        // 第一类数据收集，父组件传递的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        // 第二类：需要通过数据双向绑定v-model收集
        skuName: '',
        price: 0,
        weight: '',
        skuDesc: '',
        // 第三类：需要自己书写代码
        // 默认图片
        skuDefaultImg: '',
        // 收集图片的字段
        skuImageList: [],
        // 平台属性
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0
          // }
        ],
        // 销售属性
        skuSaleAttrValueList: []
      },
      spu: {},
      // 收集图片数据字段，缺少isDefault字段
      imageList: []
    }
  },
  methods: {
    // 获取SkuForm数据
    async getData(category1Id, category2Id, spu) {
      // 收集父组件传递的数据
      this.skuInfo.category3Id = spu.category3Id
      this.skuInfo.spuId = spu.spuId
      this.skuInfo.tmId = spu.tmId
      this.spu = spu
      // 获取图片数据
      const result = await this.$API.spu.reqSpuImageList(spu.id)
      if (result.code === 200) {
        const list = result.data
        list.forEach((item) => {
          item.isDefault = 0
        })
        this.spuImageList = list
      }
      // 获取销售属性数据
      const resultSale = await this.$API.spu.reqSpuSaleAttrList(spu.id)
      if (resultSale.code === 200) {
        this.spuSaleAttrList = resultSale.data
      }
      // 获取平台属性数据
      const resultAttrInfo = await this.$API.spu.reqAttrInfoList(category1Id, category2Id, spu.category3Id)
      if (resultAttrInfo.code === 200) {
        this.attrInfoList = resultAttrInfo.data
      }
    },
    handleCurrentChange(params) {
      this.imgageList = params
    },
    // 设置默认图片
    changeDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0
      })
      row.isDefault = 1
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    // 关添加SKU界面
    cancel() {
      this.$emit('changeScenes', 0)
      // 清空数据
      Object.assign(this._data, this.$options.data())
    },
    // 保存SKU
    async save() {
      // 整理平台属性数据方式1
      // const arr = []
      // this.attrInfoList.forEach((item) => {
      //   if (item.attrIdAndValueId) {
      //     const [attrId, valueId] = item.attrIdAndValueId.split(':')
      //     const obj = { valueId, attrId }
      //     arr.push(obj)
      //   }
      // })
      // 方式2
      const { attrInfoList, skuInfo, spuSaleAttrList, imageList } = this
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [ attrId, valueId ] = item.attrIdAndValueId.split(':')
          prev.push({ attrId, valueId })
        }
        return prev
      }, [])
      // 整理销售属性数据
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':')
          prev.push({ saleAttrId, saleAttrValueId })
        }
        return prev
      }, [])
      // 整理图片数据
      skuInfo.skuImageList = imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id
        }
      })
      // 发请求
      const result = await this.$API.spu.reqAddSku(skuInfo)
      if (result.code === 200) {
        this.$message({ type: 'success', message: '添加SKU成功' })
        this.$emit('changeScenes', 0)
        // 清空数据
        Object.assign(this._data, this.$options.data())
      }
    }
  }
}
</script>
<style scoped></style>
